import BaseEvent from '@/components/events/BaseEvent'
import { isWeibo, replaceStr, getCompIdByParam } from '@/plugins/utils'
import { CONTROL_TYPES } from '@/common/const-wb'

export default class OpenPushEvent extends BaseEvent{
  constructor() {
    super();
    this.postPush = null;
  }

  excute({item, event, postPush}){
    this.item = item;
    this.event = event;
    this.postPush = postPush;
    let type
    let text
    let pics
    let jumpUrl_
    let isWeibo_
    let pic_ids_
    let pic_ids
    let relateCompId
    //发布类型:文字(0),选图(1),拍照(2),POI(3),长微博(4),话题(6),选视频(9)
    type = event.comps[0].attrs.value
    text = event.comps[1].attrs.value || ''
    text = replaceStr(this.formValues, text);
    //获取从其他交互中传入或者是设置的值
    relateCompId = getCompIdByParam(event.comps[2].attrs.value);
    let pushData = this.getPicsAndTextByEvent(relateCompId);
    if(pushData.pics){
      pics = pushData.pics;
    }
    if(pushData.text){
      text = pushData.text;
    }


    jumpUrl_ = ''
    isWeibo_ = isWeibo()
    if (isWeibo_) {
      jumpUrl_ = 'sinaweibo://compose?'
    } else {
      jumpUrl_ = 'https://m.weibo.cn/compose?'
    }
    jumpUrl_ += 'content_type=' + type + '&content=' + encodeURIComponent(text)
    if (pics && pics.length > 0) {
      jumpUrl_ += '&pics=' + encodeURIComponent(JSON.stringify(pics))
      //自动发博
      if(!isWeibo_){
        pic_ids_ = []
        _.forEach(pics, (picItem) => {
          pic_ids_.push(picItem.pid)
        })
        pic_ids = pic_ids_.join()
        this.postPush(text, pic_ids, this.item.id)
        return
      }
    }else{
      //发博是否公开
      if(!this.push_visible){
        jumpUrl_ += "&grouptype=2"
      }else{
        jumpUrl_ += "&grouptype=0"
      }
    }
    this.openLinkBySelfUrl(jumpUrl_);
    if(isWeibo_){
      let comType = "click"
      let wModule = "push"
      this.onPostStatics(null, { comType, wModule, jumpUrl: jumpUrl_, params: text});
    }
  }

  getPicsAndTextByEvent(relateCompId){
    //打开相册中设置的数据
    let pics
    let text
    //在截图中保存的 数据
    if(this.frontData[this.item.id]){
      pics = this.frontData[this.item.id].pics
      if(this.frontData[this.item.id].text){
        text = this.frontData[this.item.id].text
      }
    }
    if(relateCompId && relateCompId.length > 0) {
      // 存在 关联组件id
      const relateCompControlData = this.controls[relateCompId];
      let pushControlData;
      // wb_push 事件
      if(relateCompControlData && relateCompControlData[CONTROL_TYPES.wb_push]){
        pushControlData = relateCompControlData[CONTROL_TYPES.wb_push];
      }
      // 获取发博图片数组
      if(pushControlData) {
        let elements = pushControlData.data.elements;
        let itemIndex
        let relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId)
        if(relateAttrCompId && relateAttrCompId.length > 0) {
          const relateAttrCompData = this.componentMap[relateAttrCompId];
          if(relateAttrCompData.lists && relateAttrCompData.lists.length > 0) {
            let vote_id = relateAttrCompData.lists[this.commonAttr.itemIndex].voteId;
            itemIndex = vote_id - 1;
          }
        } else {
          itemIndex = this.commonAttr.itemIndex >= 0 ? this.commonAttr.itemIndex : 0;
        }
        pics = elements[itemIndex].pics;
        text = elements[itemIndex].text;
        let pics_ = []
        _.forEach(pics, (pic) => {
          pics_.push({
            thumbnail: pic.pic_url,
            original: pic.pic_url,
            pid: pic.pic_id,
          })
        })
        pics = pics_
      }
    }
    return {pics, text}
  }

  destroy(){
    this.item = null;
    this.event = null;
    this.isH5Edit = false;
    this.postPush = null
  }
}
